package com.dy.lock.screen.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.ShopComponent;
import com.dy.lock.screen.constants.Constant;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.ShopCategoryInfo;
import com.dy.lock.screen.pojo.ShopInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.ShopService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.utils.LocationUtil;
import com.dy.lock.screen.vo.ShopVo;
import com.google.gson.Gson;

@Component("shopComponent")
public class ShopComponentImpl implements ShopComponent {
	private static final Logger log = Logger.getLogger(ShopComponentImpl.class);
	private Gson gson;

	@Resource(name = "shopService")
	private ShopService shopService;

	public ShopComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<ShopInfo> list(String body, Head head) {
		ShopVo vo = gson.fromJson(body, ShopVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Float lng = vo.getLng();
			Float lat = vo.getLat();
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			paraMap.put("isDelete", Constant.IS_NOT_DELETE);
			paraMap.put("shopCategory", vo.getShopCategory());
			List<ShopInfo> shopList = this.shopService.queryPage(paraMap);
			if (shopList != null && !shopList.isEmpty()) {
				for (ShopInfo shop : shopList) {
					if (StringUtils.isNoneBlank(shop.getShopHeader())) {
						shop.setShopHeader(CommonUtil.getUrlPrefix()
								+ shop.getShopHeader());
					}
					String materialTitle = this.shopService
							.queryNewestTitle(shop.getShopId());
					shop.setMaterialTitle(materialTitle);
					if (lng != null && lat != null && shop.getLng() != null
							&& shop.getLat() != null) {
						double distance = LocationUtil.calDistance(lat, lng,
								shop.getLat(), shop.getLng());
						shop.setDistance(distance);
					}
				}
				return shopList;
			} else {
				return new ArrayList<ShopInfo>();
			}

		} else {
			log.info("requestId:" + head.getRequestId() + ",获取店铺列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<ShopCategoryInfo> categoryList(String body, Head head) {
		ShopVo vo = gson.fromJson(body, ShopVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			paraMap.put("isDelete", Constant.IS_NOT_DELETE);
			List<ShopCategoryInfo> shopList = this.shopService
					.queryShopCategoryList(paraMap);
			if (shopList != null && !shopList.isEmpty()) {
				return shopList;
			} else {
				return new ArrayList<ShopCategoryInfo>();
			}

		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",获取店铺分类信息列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
